Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix custom status label bug #555

Closed
wants to merge 2 commits into from
Closed

Conversation

cojennin
Copy link
Contributor

@cojennin cojennin commented Nov 20, 2019

We should be using the status in the Save as section of the Gutenberg editor. This change will add the status to the Save as section when a user saves the post with a custom status. There's currently no way to hook into core to do this, so we have a timeout to correct the status once it's set by core.

To validate status being set

  • Create a post
  • Change it's status to "Assigned"
  • Click "Save as Assigned"
  • Confirm that after saving, "Save as Assigned" still appears

@mjangda
Copy link
Member

mjangda commented Nov 20, 2019

Note that we need to test these changes with the Classic Editor as well.

@mjangda
Copy link
Member

mjangda commented Nov 21, 2019

Aside: we should revert the changes to package-lock.json, since those aren't necessary for this PR.

@davisshaver
Copy link

Tested on site with block editor enabled for posts and can confirm this branch addresses issue described in #553. Also confirmed the Move to Trash error described above exists on master and is addressed by the same branch..

I also tested with post type that does not have block editor enabled but does have custom statuses enabled. Can confirm that bugs with scheduling and trashing posts does not seem to be present on master. Additionally, no regression with scheduling and trashing experienced was experienced with this classic editor post type when testing with fix/custom-status-scheduled.

'name' => esc_js( $status->name ),
'slug' => esc_js( $status->slug ),
'description' => esc_js( $status->description ),
$all_statuses = array_merge( $custom_statuses, $this->get_default_statuses() );

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

+1 to abstracting this into get_default_statuses()

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Still kinda confusing, but hoping to move in the right direction

* The default statuses from core. Returned as an array of stdClass to
* make it easy to merge with our custom statuses (which are WPTerm objects)
*
* return array an array of stdClass objects

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

missing @ in return

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah, good catch

// Gutenberg will also override the status set in '.editor-post-save-draft' after save, and there isn't yet a way
// to subscribe to a "post save" message. So instead set a timeout and override the text in '.editor-post-save-draft'
let schedulePostStatusUpdater = () => {
setTimeout(() => {
Copy link

@davisshaver davisshaver Nov 24, 2019

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Potentially a more "native" way of doing this would be to monitor for changes to the modified time (set modified time on load and compare/update/fire action in wp.data.subscribe callback).

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yea, not a bad thought, I'll mess around and see if I can get that working

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Though I'm not sure it'll solve the underlying issue. We can observe post update, but the problem is here.

Gutenberg is setting a timeout of 1s to flip the message from "Saving..." to the original text: "Save as Draft|Pending". But if there's concern over about polling, we could set one timeout for something like 1.2s and that might solve it (but small race condition)

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I get what you mean. The two options you proposed (callback loop and timeout after save) make sense to me based on current Gutenberg code. This seems like it could also be a good place for an upstream change to Gutenberg if there's a filter that could make it work. Best idea I have right now would be providing a way to filter/bring your own function to use instead of isPending ? __( 'Save as Pending' ) : __( 'Save Draft' );

$scheduled_status->name = __( 'Scheduled', 'edit-flow' );
$scheduled_status->slug = 'future';
$scheduled_status->description = '';

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

minor but might be an extra linebreak here?

return [ $published_status, $private_status, $scheduled_status ];
}

/***

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Minor but maybe an extra asterisk here?

@@ -452,7 +434,7 @@ function post_admin_header() {
// Now, let's print the JS vars
?>
<script type="text/javascript">
var custom_statuses = <?php echo json_encode( $all_statuses ); ?>;
var custom_statuses = JSON.parse( decodeURIComponent( '<?php echo rawurlencode( wp_json_encode( $statuses_js ) ); ?>' ) );
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This seems overly complex? Why do we need multiple levels of encoding and decoding?

If there's a valid reason, we should note that in an inline comment.

Copy link
Contributor Author

@cojennin cojennin Nov 25, 2019

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same, but it's in the suggested VIP code sample for encoding JSON. Down to move it back to just wp_json_encode if no concerns there

}

schedulePostStatusUpdater();
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm a bit worried out the unwanted (and unexpected) effects of having an infinite loop to fix the label bug (i.e. where the save button occasionally flips to "Save Draft" after saving).

It would be better to keep the change to have the save button as generic as possible (i.e. "Save") and live with the occasional text flip. And then work with the Gutenberg project to find a better way to fix this.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yea, maybe there's a better way to approach this while waiting for Gutenberg, I'll mess around a bit. Just seems like a point of confusion for consumers

@mjangda
Copy link
Member

mjangda commented Nov 25, 2019

@cojennin I have a much simpler fix in #556 that I'm going to push out. I'm going to keep this open as I think it's worth iterating on to improve the the "Save" button behavior as it's definitely not ideal right now (@davisshaver's idea is worth exploring, for example).

@cojennin
Copy link
Contributor Author

cojennin commented Nov 25, 2019 via email

@cojennin
Copy link
Contributor Author

cojennin commented Nov 25, 2019

@mjangda that fix looked good (#556)! It was what I had originally started with before trying to see if we could resolve some of these other issues

@cojennin cojennin force-pushed the fix/custom-status-scheduled branch from 1770fc4 to 09a399b Compare November 25, 2019 14:48
@cojennin
Copy link
Contributor Author

@mjangda @davisshaver doesn't seem like there's a real clean way to overcome Gutenberg explicitly setting Save as Draft or Save as Pending after a user clicks save after a 1s timeout.

I was thinking we might be able to use a MutationObserver instead of polling, but browser support isn't great (see IE), and the shim I found effectively does exactly what this PR does now (set's a timeout to observer a DOM change).

I added an attempts parameter to the timeout function to prevent looping indefinitely.

It would be great to get a fix into Gutenberg core for this issue, but in the meantime it'd be great to solve this for Edit Flow users. The behaviour we have now, where the text flips from "Save" to "Save Draft" after a user clicks save, regardless of status, is confusing.

@davisshaver
Copy link

I dropped an issue into Gutenberg, linking here for visibility: WordPress/gutenberg#18743

@cojennin
Copy link
Contributor Author

@mjangda @davisshaver I've updated this PR with some e2e testing to verify the PR works as expected, let me know how're you're feeling about it. I'm a little more confident now that it'll work as expected

@cojennin cojennin force-pushed the fix/custom-status-scheduled branch from 23e0a65 to 046814c Compare January 14, 2020 03:45
@cojennin cojennin changed the title Fix custom status scheduling issue Fix custom status label bug Jan 17, 2020
@cojennin
Copy link
Contributor Author

Closing in favor of #585

@cojennin cojennin closed this Jan 24, 2020
@cojennin cojennin deleted the fix/custom-status-scheduled branch January 29, 2020 21:33
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants